package com.enqualcomm.kids.mvp.login.wx.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.enqualcomm.kids.R;
import com.enqualcomm.kids.config.userdefault.AppDefault;
import com.enqualcomm.kids.config.userdefault.UserDefault;
import com.enqualcomm.kids.mvp.NetworkModel;
import com.enqualcomm.kids.mvp.login.wx.OauthService;
import com.enqualcomm.kids.mvp.login.wx.model.WXLoginModelImpl;
import com.enqualcomm.kids.mvp.login.wx.model.WxLoginModel;
import com.enqualcomm.kids.mvp.login.wx.view.BindPhoneView;
import com.enqualcomm.kids.mvp.login.wx.view.WXLoginView;
import com.enqualcomm.kids.network.socket.response.LoginOtherResult;

import common.utils.MyLogger;
import rx.functions.Action1;

/**
 * Created by Administrator on 2016/9/8.
 */
public class LoginPresenter implements Presenter, OnWXLoginListener {
    private static int LOGIN_WX = 1;
    private static int LOGIN_QQ = 2;
    private static int LOGIN_ZFB = 3;

    NetworkModel networkModel;
    AppDefault appDefault;
    UserDefault userDefault;
    Activity activity;
    WXLoginView view;
    WxLoginModel model;


    public LoginPresenter(Activity activity, WXLoginView view) {
        networkModel = new NetworkModel();
        appDefault = new AppDefault();
        this.activity = activity;
        this.view = view;
        model = new WXLoginModelImpl();
    }

    public void attachView(WXLoginView view) {
        this.view = view;
    }

    @Override
    public void start() {
        view.startLogin();
        MyLogger.kLog().i("=====开始绑定账号");
        OauthService oauthService = new OauthService();
        oauthService.doOauthVerifyWX(activity).subscribe(new Action1<Boolean>() {
            @Override
            public void call(Boolean aBoolean) {
                if (aBoolean) {
                    model.login(activity, LoginPresenter.this);
                } else {
                    view.waitLongTime();
                }
            }
        });
    }

    @Override
    public void loginSuccess(LoginOtherResult response) {
        loginWXSuccess(response);
    }

    @Override
    public void loginError() {
        /*view.successLogin(false);*/
        view.waitLongTime();
    }

    //TODO 如果没有手机号进入绑定手机号界面，如果有手机号则直接进入首页
    private void loginWXSuccess(LoginOtherResult response) {

        /**需要显示绑定手机号：
         * 1、type ==0 微信未注册
         * 2、type == 3 && 手机号为空  解绑手机号造成手机号为空
         * */
        if (Integer.parseInt(response.result.type) == 0 || (Integer.parseInt(response.result.type) == 3 && TextUtils.isEmpty(response.result.phone))) {
            view.successLogin(false);
        } else {
            appDefault.setUserid(response.result.userid);
            appDefault.setUserkey(response.result.userkey);
            appDefault.setProducts(response.result.products);
            userDefault = new UserDefault(response.result.userid);
            userDefault.setLoginOtherResult(response);
            appDefault.setUsername(response.result.username);
            appDefault.setApiUsername(response.result.username);
            appDefault.setWXLoginType(Integer.parseInt(response.result.type));
            view.successLogin(true);

        }
    }

}
